package cn.gson.crm.control.total;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.TotalDao;
import cn.gson.crm.model.entity.Staff;

/**
 * Servlet implementation class PaymentServlet
 */
@WebServlet("/payment")
public class PaymentServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private TotalDao td=new TotalDao();
       
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String stuid=request.getParameter("stuid");
		String totalId=request.getParameter("totalId");
		String stuclass=request.getParameter("stuclass");
		String totalMoney=request.getParameter("totalMoney");
		String totalNoMoney=request.getParameter("totalNoMoney");
		String totalStudentName=request.getParameter("stuname");
		
		List<Staff> payeeList=td.getPayees();

		request.setAttribute("stuid", stuid);
		request.setAttribute("totalId", totalId);
		request.setAttribute("stuclass", stuclass);
		request.setAttribute("payeeList", payeeList);
		request.setAttribute("totalMoney", totalMoney);
		request.setAttribute("totalNoMoney", totalNoMoney);
		request.setAttribute("totalStudentName", totalStudentName);
		forward("total/payment.jsp");
	}
	
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String stuId=request.getParameter("stuId");
		String staffId=request.getParameter("staffId");
		String totalId=request.getParameter("totalId");
		String stuclasss=request.getParameter("stuclasss");
		String payMoneys=request.getParameter("payMoneys");
		String totalMoneys=request.getParameter("totalMoneys");
		String totalNoMoneys=request.getParameter("totalNoMoneys");
		String totalStudentNames=request.getParameter("totalStudentNames");
		request.setAttribute("stuid", stuId);
		request.setAttribute("totalId", totalId);
		request.setAttribute("stuclass", stuclasss);
		request.setAttribute("payMoneys", payMoneys);
		request.setAttribute("totalMoney", totalMoneys);
		request.setAttribute("totalNoMoney", totalNoMoneys);
		request.setAttribute("totalStudentName", totalStudentNames);
		Double newNoMoneys=null;
		if(payMoneys!=null&&!payMoneys.isEmpty()){
			if(Double.parseDouble(totalNoMoneys)>=Double.parseDouble(payMoneys)){
				if (staffId!=null&&!staffId.isEmpty()) {
					newNoMoneys =Double.parseDouble(totalNoMoneys)-Double.parseDouble(payMoneys);
					if(td.totalNoMoney(newNoMoneys, totalId)){
						td.receiptHis(Double.parseDouble(payMoneys), totalId,staffId);
						redirect("total");
					}
				}else {
					List<Staff> payeeList=td.getPayees();
					request.setAttribute("payeeList", payeeList);
					request.setAttribute("error", "请选择收款人");
					forward("total/payment.jsp");
				}
			}else {
				List<Staff> payeeList=td.getPayees();
				request.setAttribute("payeeList", payeeList);
				request.setAttribute("error", "缴费金额不能超过未交金额");
				forward("total/payment.jsp");
			}
		}else {
			List<Staff> payeeList=td.getPayees();
			request.setAttribute("payeeList", payeeList);
			request.setAttribute("error", "请输入金额");
			forward("total/payment.jsp");
		}
	}
}
